<?php

namespace app\api\controller;

use alibaba\Check;
use app\common\controller\Api;
use OSS\OssClient;
use think\Config;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $site = Config::get('site');
        $data["customer_service"]["service_phone"] = $site["service_phone"];
        $data["customer_service"]["service_qrcode"] = $site["service_qrcode"];
        $data["customer_service"]["wx_service"] = $site["wx_service"];
        $data["customer_service"]["enterprise_id"] = $site["enterprise_id"];

        $data["image"]["index"] = Db::name("image")->where("status",1)->where("type",1)->select();
        $data["image"]["my"] = Db::name("image")->where("status",1)->where("type",2)->select();
        $data["image"]["notice"] = $site["notice"];
        $data["image"]["share"] = $site["share"];
        $this->success('请求成功',$data);
    }

    public function test()
    {
        //$req = (new Check)->textCheck("傻逼一个");
//        $req = (new Check)->imageCheck("https://taochewa.oss-cn-beijing.aliyuncs.com/uploads/20240712/b98dff9bbb3421a6f56bdb163b1ff3fb.jpg");
//        $req = (new Check)->videoCheck("https://taochewa.oss-cn-beijing.aliyuncs.com/uploads/20240403/%E5%B1%8F%E5%B9%95%E5%BD%95%E5%88%B6%202024-07-12%20145533.mp4");
        $req = (new Check)->videoModerationResult("vi_f_4SP1wLCPgs4VucipPf7Ocl-1AamXg");
        dump($req);
    }


    public function transcoding()
    {
        // 锁文件路径
        $lockFile = __DIR__ . '/task.lock';

        // 尝试获取锁
        $fp = fopen($lockFile, 'w');
        if (!$fp) {
            die("无法打开锁文件");
        }

        // 尝试获取独占锁
        if (!flock($fp, LOCK_EX | LOCK_NB)) {
            fclose($fp);
            die("任务正在运行，无法获取锁");
        }

        try {
            $config = get_addon_config('alioss');
            $oss = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint']);


            $list = Db::name("video")->where("status",0)->select();
            foreach ($list as $v) {
                $filePath = $v["file"];
                $getSaveName = $v["name"];
                $url = $v["url"];
                $arr = explode(".", $filePath);
                $outputFile = "";
                if($arr[1] == "mp4"){
                    // 输入视频文件路径
                    $inputFile = $filePath;
                    $destDir = explode($getSaveName,$filePath)[0]."output/";
                    if (!is_dir($destDir)) {
                        @mkdir($destDir, 0755, true);
                    }
                    // 输出 MP4 文件路径
                    $outputFile = $destDir.$getSaveName;
                    // FFmpeg 命令
                    $command = "ffmpeg -i $inputFile $outputFile";
                    // 执行 FFmpeg 命令
                    exec($command, $output, $returnCode);
                    // 检查执行结果
                    if ($returnCode === 0) {
                        $ret = $oss->uploadFile($config['bucket'], ltrim($url, "/"), $outputFile);
                        @unlink($filePath);
                        @unlink($outputFile);
                        Db::name("video")->where("id", $v["id"])->delete();
                    } else {
                        Db::name("video")->where("id", $v["id"])->update(["status"=>1]);
                    }
                }



            }
            $this->success("完成");
        } catch (Exception $e) {
            echo "捕获到异常: " . $e->getMessage();
            $this->error("出错了");
            // 捕获并处理异常
        } finally {
            // 释放锁
            flock($fp, LOCK_UN);
            fclose($fp);
            // 删除锁文件
            unlink($lockFile);

        }





    }






}
